<?php

namespace App\Models\Teaching;

use App\Models\File;
use App\Models\Traits\HasManyFiles;
use Illuminate\Database\Eloquent\Model;

class HomeworkInfo extends Model
{
    protected $table = 'homework_info';
    protected $guarded = [];
    protected $hidden = [
        'updated_at'
    ];
    protected $appends = [
        'resource_set'
    ];

    public function getResourceSetAttribute(){
        $resource = !empty($this->attributes['resource'])?$this->attributes['resource']:'';
        if(!$resource){
            return [];
        }

        $files = json_decode($resource, true);
        foreach($files as $k => $f) {
            $file = File::whereId($f['id'])->first();

            $files[$k]['file_info'] = $file->toArray();
            $files[$k]['file_origin'] = [
                'file_name' => $f['fileName'],
                'size' => $file->file_size,
                'url' => $file->file_dir . $file->file_path,
                'id' => $f['id']
            ];
        }

        return $files;
    }
}
